package com.wangjiyuan.giftshuo.ui.activity.other;

import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import com.google.gson.Gson;
import com.wangjiyuan.giftshuo.R;
import com.wangjiyuan.giftshuo.base.App;
import com.wangjiyuan.giftshuo.bean.cache.JsonBean;
import com.wangjiyuan.giftshuo.bean.cache.JsonBeanDao;
import com.wangjiyuan.giftshuo.bean.home.banners.ADBean;
import com.wangjiyuan.giftshuo.bean.home.preset.PresetBean;
import com.wangjiyuan.giftshuo.bean.home.secondarybanners.SecondaryBanners;
import com.wangjiyuan.giftshuo.http.HttpInterfaces;
import com.wangjiyuan.giftshuo.http.HttpUtils;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class WelcomeActivity extends AppCompatActivity {
    HttpInterfaces httpInterfaces;
    Intent intent;
    Bundle bundle;
    Gson gson;
    JsonBeanDao jsonBeanDao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        gson = new Gson();
        jsonBeanDao = App.app.getDaoSession().getJsonBeanDao();
        JsonBeanDao.createTable(App.app.getDb(), true);
        ConnectivityManager manager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = manager.getActiveNetworkInfo();
        if (activeNetworkInfo!=null) {
            getADBean();
        } else {

            List<JsonBean> jsonBean = jsonBeanDao.queryBuilder().list();
            Intent intent = new Intent(WelcomeActivity.this, MainActivity.class);
//            App.app
            ADBean adBean = gson.fromJson(jsonBean.get(0).getJson(), ADBean.class);
            SecondaryBanners secondaryBanners = gson.fromJson(jsonBean.get(1).getJson(), SecondaryBanners.class);
            PresetBean presetBean = gson.fromJson(jsonBean.get(2).getJson(), PresetBean.class);
            Bundle bundle = new Bundle();
            bundle.putSerializable("ad", (Serializable) adBean.getData().getBanners());
            bundle.putSerializable("ba", (Serializable) secondaryBanners.getData()
                    .getSecondaryBanners());
            bundle.putSerializable("pr", (Serializable) presetBean.getData().getChannels());
            intent.putExtra("bundle", bundle);
            startActivity(intent);
        }
    }


    private void getADBean() {
        httpInterfaces = HttpUtils.getInstance().getHttpInterfaces();
        httpInterfaces.getADBean().observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Subscriber<ADBean>() {
                    @Override
                    public void onCompleted() {
                        getSecBean();
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e("e", e.getMessage());
                    }

                    @Override
                    public void onNext(ADBean adBean) {
                        jsonBeanDao.deleteAll();
                        String s = gson.toJson(adBean);
                        JsonBean jsonBean = new JsonBean();
                        jsonBean.setJson(s);
                        jsonBeanDao.insert(jsonBean);
                        intent = new Intent(WelcomeActivity.this, MainActivity.class);
                        bundle = new Bundle();
                        bundle.putSerializable("ad", (Serializable) adBean.getData().getBanners());

                    }
                });
    }

    private void getSecBean() {
        Map<String, String> map = new HashMap<>();
        map.put("gender", "2");
        map.put("generation", "0");
        httpInterfaces.getSecondaryBean(map).observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Subscriber<SecondaryBanners>() {
                    @Override
                    public void onCompleted() {
                        getPre();
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e("ee", e.toString());
                    }

                    @Override
                    public void onNext(SecondaryBanners secondaryBanners) {
                        String s = gson.toJson(secondaryBanners);
                        JsonBean jsonBean = new JsonBean();
                        jsonBean.setJson(s);
                        jsonBeanDao.insert(jsonBean);
                        bundle.putSerializable("ba", (Serializable) secondaryBanners.getData()
                                .getSecondaryBanners());
                        intent.putExtra("bundle", bundle);

                    }
                });
    }

    private void getPre() {
        Map<String, String> map = new HashMap<>();
        map.put("gender", "2");
        map.put("generation", "0");
        httpInterfaces.getPreset(map).observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Subscriber<PresetBean>() {
                    @Override
                    public void onCompleted() {
                        startActivity(intent);
                        finish();
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e("eee", e.toString());
                    }

                    @Override
                    public void onNext(PresetBean presetBean) {
                        String s = gson.toJson(presetBean);
                        JsonBean jsonBean = new JsonBean();
                        jsonBean.setJson(s);
                        jsonBeanDao.insert(jsonBean);
                        bundle.putSerializable("pr", (Serializable) presetBean.getData().getChannels());
                    }
                });
    }
}
